package com.userfactssample.client.client.screens.navigator;

import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ScrollEvent;
import com.smartgwt.mobile.client.types.Alignment;
import com.smartgwt.mobile.client.widgets.BaseButton.ButtonType;
import com.userfactssample.client.client.framework.UMButton;
import com.userfactssample.client.client.framework.UMHLayout;
import com.userfactssample.client.client.framework.UMLink;
import com.userfactssample.client.client.framework.UMScrollablePanel;
import com.userfactssample.client.client.framework.UMVLayout;
import com.userfactssample.client.client.framework.event.UMActionEvent;
import com.userfactssample.client.client.framework.handler.UMActionHandler;

public abstract class AbstractUMNavigator extends UMScrollablePanel {

	protected UMVLayout mainScreenLayout = new UMVLayout();
	UMVLayout bottomScreenLayout = new UMVLayout();
	
	UMHLayout navigatorLayout = new UMHLayout();
	UMHLayout footerScreenLayout = new UMHLayout();
	private int Navigator_TOP;
	protected UMButton backButton;
	protected UMButton nextButton;
	
	public AbstractUMNavigator() {
		createLayout();
		createComponents();
		adjustNavigatorBar();
	}
	
	private void adjustNavigatorBar() {
		Navigator_TOP = isIPhone() && !isStandAlone() ? -10 : 120;
		bottomScreenLayout.setPosition(Position.ABSOLUTE);
		bottomScreenLayout.setWidth("100%");
		bottomScreenLayout.setTop(Window.getClientHeight() - Navigator_TOP);
		sinkEvents(Event.ONCLICK | Event.ONMOUSEWHEEL | Event.GESTUREEVENTS | Event.TOUCHEVENTS | Event.MOUSEEVENTS | Event.FOCUSEVENTS | Event.KEYEVENTS);        
		
		Window.addWindowScrollHandler(new Window.ScrollHandler() {
			@Override
			public void onWindowScroll(ScrollEvent event) {
				bottomScreenLayout.setTop(Window.getClientHeight()-Navigator_TOP);
				mainScreenLayout.setHeight(Window.getClientHeight()-Navigator_TOP);
			}
        });
        Window.addResizeHandler(new ResizeHandler() {
            @Override
            public void onResize(ResizeEvent event) {
            	bottomScreenLayout.setTop(Window.getClientHeight()-Navigator_TOP);
            }
        });
	}

	protected void createLayout(){
		getStyle().setBackgroundColor("black");
	}
	
	protected void createComponents(){
		addMember(mainScreenLayout.getUiComponent());
		
		buildBottomScreen();
		addMember(bottomScreenLayout.getUiComponent());;
	}
	
	private void buildBottomScreen() {
		bottomScreenLayout.addMember(createNavigator());
		bottomScreenLayout.addMember(createFooter());
	}
	
	private UMHLayout createNavigator() {
		navigatorLayout.setAlign(Alignment.JUSTIFY);
		
		backButton = new UMButton();
		backButton.setWidth("30%");
		backButton.setButtonType(ButtonType.ROUNDED_RECTANGLE);
		navigatorLayout.addMember(backButton);
		
		nextButton = new UMButton();
		nextButton.setWidth("30%");
		nextButton.setButtonType(ButtonType.ROUNDED_RECTANGLE);
		navigatorLayout.addMember(nextButton);
		
		backButton.addClickHandler(new UMActionHandler() {
			public void onAction(UMActionEvent event) {
				navigateToPrevious();
			}
		});
		
		nextButton.addClickHandler(new UMActionHandler() {
			@Override
			public void onAction(UMActionEvent event) {
				navigateToNext();
			}
		});
		
		return navigatorLayout;
	}

	protected abstract void navigateToNext();

	protected abstract void navigateToPrevious();
	
	private UMHLayout createFooter() {
		footerScreenLayout.setAlign(Alignment.CENTER);
		footerScreenLayout.setWidth("100%");
		footerScreenLayout.setMembersMargin(10);
		
		UMLink aboutLabel = new UMLink("http://www.userfacts.com/", "color:white;", "About the survey" );
		UMLink privacyLabel = new UMLink("http://www.userfacts.com/", "color:white;", "Privacy policy" );

		footerScreenLayout.addMember(aboutLabel);
		footerScreenLayout.addMember(privacyLabel);
		return footerScreenLayout;
	}
}
